package com.rupertjones.globalcron.web;

import com.rupertjones.globalcron.domain.Host;
import com.rupertjones.globalcron.web.model.DisplayableJob;
import com.rupertjones.globalcron.web.service.JobService;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import org.springframework.stereotype.Controller;

import java.util.Collection;

/**
 * <p>&copy Rupert Jones 2011,2012</p>
 *
 * @author rup
 */
@Controller
public class InfoHostAction extends AbstractAction {

    private Host host;

    @SpringBean
    private JobService jobService;

    @Before(stages = LifecycleStage.BindingAndValidation)
    public void loadAndBind() {
        String id = getContext().getRequest().getParameter("host.id");
        int hostId = Integer.parseInt(id);
        host = getJobService().getHost(hostId);
    }

    public Host getHost() {
        return host;
    }

    @DefaultHandler
    public Resolution view() {
        return new ForwardResolution("/WEB-INF/jsp/info_host.jsp");
    }

    public Collection<DisplayableJob> getEnabledJobs() {
        return jobService.getEnabledJobs(host);
    }

    public Collection<DisplayableJob> getDisabledJobs() {
        return jobService.getDisabledJobs(host);
    }
}
